// packageA/member/supplier/SupplierIndex/SupplierIndex.js
const app = getApp();
let location = require("../../../../mybehaviors/location/location");
Page({
  behaviors: [location],
  /**
   * 页面的初始数据
   */
  data: {
    info: {},
    keyword: '',
    page: 1,
    is_open:1,
    show_hot: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '供应商列表',
    });
    this._getLocation((mapdata, e) => {
      this.point = app.qqMapTransBMap(parseFloat(e.lng),parseFloat(e.lat));
      this.getClass();
    },(err)=>{
      //拒绝定位
      wx.showToast({
        title:'定位失败',
        icon:'none',
        duration:2000,
        success:function(res){}
      });
      this.getClass();
    });
    
    let language = wx.getStorageSync("langIndex");
    this.setData({
      language: language.en,
    });
  },
  goUrl(e){
    let link = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: link,
    });
  },
  goShop(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/packageA/member/supplier/SupplierShop/SupplierShop?id=' + id,
    });
  },
  tabrshowbtn(e) {
    let bol = e.detail;
    if (bol) {
      this.setData({
        kFootshow: false
      });
    }
  },
  tapinp(e) {
    let val = e.detail.value;
    this.setData({
      keyword: val,
      page: 1
    });
  },
  tapConfirm(){
    this.getClass();
  },
  goToAdress(e) {
    let latitude = e.currentTarget.dataset.item.area.lat;
    let longitude = e.currentTarget.dataset.item.area.lng;
    let store_name = e.currentTarget.dataset.item.store_name;
    let point = app.BMapTransqqMap(parseFloat(longitude), parseFloat(latitude));
    wx.openLocation({
      latitude: point.lat,
      longitude: point.lng,
      scale: 18,
      name: store_name
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.current_page >= this.data.last_page) {
      return;
    } else {
      this.setData({
        page: this.data.page + 1
      });
      this.getClassMore();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return{
      title:this.data.info.share?this.data.info.share.share_title:'',
      imageUrl:this.data.info.share?this.data.info.share.share_img:'',
      path:'packageA/member/supplier/SupplierIndex/SupplierIndex'
    };
  },
  onShareTimeline:function(){
    return{
      title:this.data.info.share?this.data.info.share.share_title:'',
      imageUrl:this.data.info.share?this.data.info.share.share_img:'',
      path:'packageA/member/supplier/SupplierIndex/SupplierIndex'
    };
  },
  getClass() {
    wx.showLoading({
      title: '加载中',
    });
    let urlStr = app.getNetAddresss("plugin.supplier.frontend.shop.index.getSupplierList");
    let json = {
      keyword: this.data.keyword,
      page: this.data.page,
      lng: this.point.lng, 
      lat: this.point.lat
    };
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        wx.hideLoading({
          success: (res) => {},
        });
        var res = resdata.data;
        if (res.result == 1) {
          if(!res.data.is_open){
            wx.showToast({
              title:'插件未开启',
              icon:'none',
              duration:2000,
              success:function(res){
                setTimeout(() => {
                  wx.redirectTo({
                    url: '/packageG/index/index',
                  });
                }, 2000);
              }
            });
          }
          this.setData({
            is_open:res.data.is_open,
            info: res.data,
            listData: res.data.list.data,
            current_page: res.data.list.current_page,
            last_page: res.data.list.last_page,
            show_hot: res.data.show_hot
          });
        } else {
          wx.showToast({
            title: res.msg,
          });
        }
      }
    });
  },
  getClassMore() {
    wx.showLoading({
      title: '加载中',
    });
    let urlStr = app.getNetAddresss("plugin.supplier.frontend.shop.index.getSupplierList");
    let json = {
      keyword: this.data.keyword,
      page: this.data.page,
      lng: this.point.lng, 
      lat: this.point.lat
    };
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        wx.hideLoading({
          success: (res) => {},
        });
        var res = resdata.data;
        if (res.result == 1) {
          this.setData({
            listData: this.data.listData.concat(res.data.list.data),
            current_page: res.data.list.current_page,
            last_page: res.data.list.last_page
          });
        }else{
          wx.showToast({
            title: res.msg,
          });
        }
      }
    });
  }
});